<?php

namespace App\Lib;

use App\Models\Setting;
use EasyWeChat\Factory;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

class WxMapHandle
{
    /**
     * @param $mall_id
     * @return \EasyWeChat\OfficialAccount\Application
     */
    public static function getOfficialAccount($mall_id = 0)
    {
        $config = config('weixin');
        if($mall_id > 0){
            $wx_conf = Setting::query()->where('mall_id',$mall_id)->first();
            if(empty($wx_conf)){
                abort(403,'系统未配置');
            }
            $config['app_id'] = $wx_conf->wx_appid;
            $config['secret'] = $wx_conf->wx_appsecret;
            $config['token'] = $wx_conf->wx_token;
            $config['aes_key'] = $wx_conf->wx_sea_key;
        }

        return Factory::officialAccount($config);
    }
}
